package com.cn.kehong.service;

import com.cn.kehong.api.IProductThickPeriodService;
import com.cn.kehong.dao.ProductThickPeriodMapper;
import com.cn.kehong.dao.ProductThickPeriodTimeMapper;
import com.cn.kehong.domain.ProductThickPeriod;
import com.cn.kehong.domain.ProductThickPeriodExample;
import com.cn.kehong.domain.ProductThickPeriodTime;
import com.cn.kehong.domain.ProductThickPeriodTimeExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductThickPeriodServiceImpl implements IProductThickPeriodService {

    @Autowired
    private ProductThickPeriodMapper productThickPeriodMapper;

    @Autowired
    private ProductThickPeriodTimeMapper productThickPeriodTimeMapper;

    @Override
    public List<ProductThickPeriod> getProductThickPeriodList(QueryForm queryForm) throws Exception {
        int count = productThickPeriodMapper.selectProductThickPeriodCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductThickPeriod> productThickPeriods = productThickPeriodMapper.selectProductThickPeriods(queryForm);
        productThickPeriods.get(0).setCount(count);
        return productThickPeriods;
    }

    @Override
    public List<ProductThickPeriod> getProductThickPeriodListByStatus() throws Exception {
        List<ProductThickPeriod> productThickPeriods = productThickPeriodMapper.selectProductThickByStatus();
        return productThickPeriods;
    }

    @Override
    public List<ProductThickPeriodTime> getProductThickTimeByStatus() throws Exception {
        List<ProductThickPeriodTime> productThickPeriodTimes = productThickPeriodTimeMapper.selectProductThickLineTaskByLineStatus();
        return productThickPeriodTimes;
    }

    @Override
    public String addProductThickPeriod(String peiId,String productCode,String factoryName,String startCode) throws Exception {
        if(StringUtils.isEmpty(peiId)||StringUtils.isEmpty(productCode)||StringUtils.isEmpty(factoryName)){
            throw new Exception("peiId or productCode or factoryName is null");
        }
        ProductThickPeriodExample example = new ProductThickPeriodExample();
        example.createCriteria().andPeiIdEqualTo(peiId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = productThickPeriodMapper.countByExample(example);
        if(existCount > 0){
            throw new Exception("ProductThickPeriod data has exist");
        }
        String id = CommonUtil.generateRandomNum("thick-");
        ProductThickPeriod productThickPeriod = new ProductThickPeriod();
        productThickPeriod.setId(id);
        productThickPeriod.setFactoryName(factoryName);
        productThickPeriod.setProductCode(productCode);
        productThickPeriod.setPeiId(peiId);
        productThickPeriod.setStartCode(startCode);
        int count = productThickPeriodMapper.insertSelective(productThickPeriod);
        if(count < 1){
            throw new Exception("insert thick data error");
        }
        return "";
    }

    @Override
    public String updateBatchProductThickPeriod(List<ProductThickPeriod> productThickPeriods) throws Exception {
        if(CollectionUtils.isEmpty(productThickPeriods)){
            return "数组为空";
        }
        List<ProductThickPeriodTime> productThickPeriodTimes = Lists.newArrayList();
        for(ProductThickPeriod productThickPeriod:productThickPeriods){
            if(CollectionUtils.isNotEmpty(productThickPeriod.getProductThickPeriodTimes())){
                productThickPeriodTimes.addAll(productThickPeriod.getProductThickPeriodTimes());
            }
        }
        int count = productThickPeriodMapper.batchUpdateThickPeriods(productThickPeriods);
        if(count < 1){
            return "更新细磨表异常！";
        }
//        if(CollectionUtils.isNotEmpty(productThickPeriodTimes)){
//            count = productThickPeriodTimeMapper.batchInsertThickTime(productThickPeriodTimes);
//            if(count < 1){
//                throw new Exception("更新细磨表异常!!");
//            }
//        }
        return "";
    }

    @Override
    public String updateBatchProductThickPeriodTime(List<ProductThickPeriodTime> productThickPeriodTimes) throws Exception {
        if(CollectionsUtil.isEmpty(productThickPeriodTimes)){
            return "";
        }
        List<String> thickIds = Lists.newArrayList();
        for(ProductThickPeriodTime periodTime:productThickPeriodTimes){
            thickIds.add(periodTime.getThickId());
        }
        ProductThickPeriodTimeExample example = new ProductThickPeriodTimeExample();
        example.createCriteria().andThickIdIn(thickIds);
        int count = productThickPeriodTimeMapper.deleteByExample(example);
        if(count < 0){
            return "删除错误！";
        }
        count = productThickPeriodTimeMapper.batchInsertThickTime(productThickPeriodTimes);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductThickPeriod> getProductThickPeriodBackList(QueryForm queryForm) throws Exception {
        int count = productThickPeriodMapper.getProductThickPeriodBackCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductThickPeriod> productThickPeriods = productThickPeriodMapper.getProductThickPeriodBackList(queryForm);
        productThickPeriods.get(0).setCount(count);
        return productThickPeriods;
    }

    @Override
    public List<ProductThickPeriod> getXunTask() throws Exception {
        return productThickPeriodMapper.selectXunTask();
    }

    @Override
    public List<ProductThickPeriod> getLineTask() throws Exception {
        return productThickPeriodMapper.selectLineTask();
    }

    @Override
    public List<ProductThickPeriodTime> getUnDoneProductThickLineTaskByIds(List<ProductThickPeriodTime> productThickPeriodTimes) throws Exception {
        if(CollectionsUtil.isEmpty(productThickPeriodTimes)){
            return Lists.newArrayList();
        }
        Set<String> thickIds = Sets.newHashSet();
        Map<String,List<String>> numberMaps = Maps.newHashMap();
        for(ProductThickPeriodTime periodTime:productThickPeriodTimes){
            thickIds.add(periodTime.getThickId());
            if(numberMaps.containsKey(periodTime.getThickId())){
                numberMaps.get(periodTime.getThickId()).add(periodTime.getNumberTime());
            }else{
                List<String> numbers = Lists.newArrayList();
                numbers.add(periodTime.getNumberTime());
                numberMaps.put(periodTime.getThickId(),numbers);
            }
        }
        ProductThickPeriodTimeExample example = new ProductThickPeriodTimeExample();
        example.createCriteria().andThickIdIn(new ArrayList<>(thickIds));
        List<ProductThickPeriodTime> periods = productThickPeriodTimeMapper.selectByExample(example);
        Iterator<ProductThickPeriodTime> iterator = periods.iterator();
        while (iterator.hasNext()){
            ProductThickPeriodTime next = iterator.next();
            if(!numberMaps.containsKey(next.getThickId())){
                iterator.remove();
                continue;
            }
            List<String> numbers = numberMaps.get(next.getThickId());
            if(!numbers.contains(next.getNumberTime())){
                iterator.remove();
                continue;
            }
        }
        return periods;
    }


    @Override
    public String updateThickPeriodByIds(String type,List<String> ids) throws Exception {
        if(CollectionsUtil.isEmpty(ids)){
            return "";
        }
        ProductThickPeriodExample example = new ProductThickPeriodExample();
        example.createCriteria().andIdIn(ids);
        ProductThickPeriod update = new ProductThickPeriod();
        if(StringUtils.equals(type,"1")){
            update.setLtask("1");
        }else{
            update.setXjian("1");
        }
        int count = productThickPeriodMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateThickPeriodLineStatus(List<ProductThickPeriodTime> productThickTimePeriods,String status) throws Exception {
        for(ProductThickPeriodTime thickPeriodTime:productThickTimePeriods){
            thickPeriodTime.setLineStatus(status);
        }
        int count = productThickPeriodTimeMapper.batchUpdateThickPeriodsLineStatus(productThickTimePeriods);
        return CommonUtil.outStr(count);
    }
}
